package com.yungu.swift.assets.passenger.service.impl;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import com.google.common.collect.Lists;
import com.yungu.swift.assets.passenger.dao.PassengerRechargeCityMapper;
import com.yungu.swift.assets.passenger.dao.PassengerRechargeMapper;
import com.yungu.swift.assets.passenger.model.dto.PassengerRechargeCityDto;
import com.yungu.swift.assets.passenger.model.dto.PassengerRechargeDto;
import com.yungu.swift.assets.passenger.model.param.AdminPassengerRechargeParam;
import com.yungu.swift.assets.passenger.service.PassengerRechargeCityService;
import com.yungu.swift.base.ResponseData;
import com.yungu.swift.base.mapper.IMapper;
import com.yungu.swift.base.service.impl.BaseServiceImpl;
import com.yungu.swift.system.sys.model.dto.SysCityDto;
import com.yungu.swift.system.sys.service.SysCityService;
import com.yungu.swift.utils.MapUtils;
import com.yungu.swift.utils.StringUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.*;


/**
 * PassengerRechargeCityServiceImpl
 **/

@Service
public class PassengerRechargeCityServiceImpl extends BaseServiceImpl<PassengerRechargeCityDto> implements PassengerRechargeCityService {

    @Reference
    private SysCityService sysCityService;
    @Autowired
    private PassengerRechargeMapper passengerRechargeMapper;
    @Autowired
    private PassengerRechargeCityMapper passengerRechargeCityMapper;

    @Override
    protected IMapper<PassengerRechargeCityDto> getMapper() {
        return passengerRechargeCityMapper;
    }

    @Override
    public ResponseData<Boolean> savaPassengerRechargeCity(AdminPassengerRechargeParam rechargeParam) {
        Date date = new Date();
        if (StringUtils.isNotEmpty(rechargeParam.getCityUuid())) {
            //指定城市的话维护城市充值表
            //1、先判断这个城市是否有这个模板
            Map<String, Object> conditionMap = MapUtils.build(5);
            conditionMap.put("cityUuid", rechargeParam.getCityUuid());
            conditionMap.put("passengerRechargeUuid", rechargeParam.getUuid());
            conditionMap.put("status", 1);
            List<PassengerRechargeCityDto> list = getMapper().list(conditionMap);
            if (CollectionUtils.isNotEmpty(list)) {
                //存在这个城市模板，把这个城市模板的状态改成0
                PassengerRechargeCityDto passengerRechargeCityDto = new PassengerRechargeCityDto();
                passengerRechargeCityDto.setCityUuid(rechargeParam.getCityUuid());
                passengerRechargeCityDto.setPassengerRechargeUuid(rechargeParam.getUuid());
                passengerRechargeCityDto.setStatus(0);
                passengerRechargeCityDto.setUpdateBy(rechargeParam.getUserUuid());
                passengerRechargeCityDto.setUpdateOn(date);
                passengerRechargeCityMapper.edit(passengerRechargeCityDto);
            }
            //为了保存历史纪录，不管编辑还是新增都 增加一条记录。
            PassengerRechargeCityDto rechargeCityDto = new PassengerRechargeCityDto();
            rechargeCityDto.setUuid(StringUtils.buildUUID());
            rechargeCityDto.setCityUuid(rechargeParam.getCityUuid());
            rechargeCityDto.setPassengerRechargeUuid(rechargeParam.getUuid());
            rechargeCityDto.setStatus(1);
            rechargeCityDto.setMoney(rechargeParam.getMoney());
            rechargeCityDto.setGift(rechargeParam.getGift());
            rechargeCityDto.setCreateBy(rechargeParam.getUserUuid());
            rechargeCityDto.setCreateOn(date);
            return this.add(rechargeCityDto);
        } else {
            //没指定城市的话维护 passengerRecharge
            PassengerRechargeDto passengerRechargeDto = new PassengerRechargeDto();
            passengerRechargeDto.setUuid(rechargeParam.getUuid());
            passengerRechargeDto.setMoney(rechargeParam.getMoney());
            passengerRechargeDto.setGift(rechargeParam.getGift());
            passengerRechargeDto.setUpdateBy(rechargeParam.getUserUuid());
            passengerRechargeDto.setUpdateOn(date);
            return ResponseData.buildSuccessResponse(passengerRechargeMapper.edit(passengerRechargeDto) > 0);
        }

    }

    @Override
    public ResponseData<Collection<PassengerRechargeDto>> findPassengerRechargeCity(String cityUuid) {
        Map<String, PassengerRechargeDto> resultMap = new HashMap<>(9);
        //1、先得到模板中的所有值
        List<PassengerRechargeDto> passengerRechargeDtos = passengerRechargeMapper.list(null);
        if (CollectionUtils.isNotEmpty(passengerRechargeDtos)) {
            for (PassengerRechargeDto passengerRechargeDto : passengerRechargeDtos) {
                passengerRechargeDto.setPassengerRechargeUuid(passengerRechargeDto.getUuid());
                resultMap.put(passengerRechargeDto.getUuid(), passengerRechargeDto);
            }
        }
        if (StringUtils.isNotEmpty(cityUuid)) {
            //得到所有父类的CityUuid
            List<String> allCity = getAllCity(Lists.newArrayList(), cityUuid);
            //2、从最大的父类开始依次遍历所有子类
            for (int i = allCity.size() - 1; i >= 0; i--) {
                String uuid = allCity.get(i);
                Map<String, Object> conditionMap = new HashMap<>(3);
                conditionMap.put("cityUuid", uuid);
                conditionMap.put("status", 1);
                List<PassengerRechargeCityDto> rechargeCityDtos = getMapper().list(conditionMap);
                if (CollectionUtils.isNotEmpty(rechargeCityDtos)) {
                    for (PassengerRechargeCityDto passengerRechargeCityDto : rechargeCityDtos) {
                        PassengerRechargeDto passengerRechargeDto = new PassengerRechargeDto();
                        passengerRechargeDto.setUuid(passengerRechargeCityDto.getPassengerRechargeUuid());
                        passengerRechargeDto.setMoney(passengerRechargeCityDto.getMoney());
                        passengerRechargeDto.setGift(passengerRechargeCityDto.getGift());
                        passengerRechargeDto.setPassengerRechargeUuid(passengerRechargeCityDto.getUuid());
                        resultMap.put(passengerRechargeDto.getUuid(), passengerRechargeDto);
                    }
                }
            }
        }
        //3、Map转Collection
        Collection<PassengerRechargeDto> values = resultMap.values();
        return ResponseData.buildSuccessResponse(values);
    }

    /**
     * 递归实现得到某个城市uuid的所有父类城市
     *
     * @param list
     * @param cityUuid
     * @return
     */
    private List<String> getAllCity(List<String> list, String cityUuid) {
        SysCityDto sysCityDto = sysCityService.get(cityUuid).getData();
        if (sysCityDto != null) {
            list.add(sysCityDto.getUuid());
            String father = sysCityDto.getFather();
            if (null == father) {
                return list;
            }
            String fatherUuid = sysCityService.getCityUuidByAdCode(father).getData();
            if (null == fatherUuid) {
                return list;
            }
            getAllCity(list, fatherUuid);
        }
        return list;
    }
}